import { Component, OnInit, Injector, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    CodeItemServiceProxy,
    CodeRuleEditDto
} from '@shared/service-proxies/stock-service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/primeng';
import { Table } from 'primeng/table';
import { TreeNode } from 'primeng/api';
import { ArrayToTreeConverterService } from '@shared/utils/array-to-tree-converter.service';
import { ModalDirective } from 'ngx-bootstrap';
@Component({
    selector: 'codeRulesNumModal',
    templateUrl: './code-nrules.component.html'
})
export class CodeRulesNumComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('createOrEditModal') modal: ModalDirective;
    constructor(
        injector: Injector,
        private _codeRuleService: CodeItemServiceProxy,
        private _arrayToTreeConverterService: ArrayToTreeConverterService
    ) {
        super(injector);
    }

    @Input() title = '';

    @Output() modifyColsAndDatas = new EventEmitter<object>();
    files: TreeNode[];
    filterText = '';
    selectedDate = new Date();
    cols: any[];
    datas: any[];
    fileName: string;
    totalUnitCount = 0;
    treeData: any;
    coderule: CodeRuleEditDto = new CodeRuleEditDto();
    active = false;
    saving = false;
    closed = false;
    @Output() modalSave = new EventEmitter<any>();
    ngOnInit(): void {}
    show(): void {
        this.active = true;
        this.closed = false;
        this.modal.show();
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
    onUpload(event) {
        this.fileName = event.fileName;
        let fileDatas = event.fileDatas as Array<any>;

        this.primengTableHelper.showLoadingIndicator();
    }

    getCodeRulesByFilter(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._codeRuleService
            .getCodeRules(
                this.filterText,
                'num',
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.totalUnitCount = result.items.length;
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    onHidden(event) {
        this.closed = true;
    }
    rowSelect(event) {
        this.close();
        // this.modalEl.nativeElement.
        // (this.modalEl.nativeElement as any).hasClass

        let timer = setInterval(() => {
            if (this.closed) {
                this.modalSave.emit(event);
                clearInterval(timer);
            }
        }, 50);
    }
    createCodeRule(): void {}
    updateCodeRule(event): void {}
    deleteCodeRule(event): void {
        let self = this;
        self.message.confirm(self.l('提示', event.Id), this.l('确定删除？'), isConfirmed => {
            if (isConfirmed) {
                this._codeRuleService.deleteCodeRule(event.Id).subscribe(() => {
                    this.getCodeRulesByFilter();
                    abp.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }
}
